package com.yungu.swift.report.controller;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminReportParam;
import com.yungu.swift.report.dao.ReportOperationDayDao;
import com.yungu.swift.report.entity.ReportOperationDay;
import com.yungu.swift.report.entity.vo.OperationDayTotalDataVo;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report/operationDay")
public class OperationDayCtrl {

    @Autowired
    private ReportOperationDayDao reportOperationDayDao;

    @PostMapping("/queryPage")
    public ResponseData<PageVo<ReportOperationDay>> queryPage(@RequestBody AdminReportParam reportParam){
        PageBounds pageBounds = new PageBounds(reportParam.getPageNum(), reportParam.getPageSize());
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("startTime",reportParam.getStartTime());
        paramMap.put("endTime",reportParam.getEndTime());
        PageList<ReportOperationDay> pageList = reportOperationDayDao.queryPage(paramMap,pageBounds);
        PageVo<ReportOperationDay> pageVo = new PageVo(reportParam.getPageNum(), reportParam.getPageSize(), pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    /**
     * 获取总计数据 总金额等
     * @param reportParam
     * @return
     */
    @PostMapping("/getTotalData")
    public ResponseData<OperationDayTotalDataVo> getTotalData(@RequestBody AdminReportParam reportParam){
        return ResponseData.buildSuccessResponse(reportOperationDayDao.getTotalData(reportParam));
    }

    @RequestMapping("/export")
    public void exportEvaluationInfo(HttpServletResponse response, @RequestBody AdminReportParam reportParam) throws Exception {
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("startTime",reportParam.getStartTime());
        paramMap.put("endTime",reportParam.getEndTime());

        List<ReportOperationDay> list = reportOperationDayDao.queryList(paramMap);

        OutputStream os = response.getOutputStream();
        try {
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet();
            //宽度5.38
            sheet.setColumnWidth(0, ((5 + 1) * 7 + 5) / 7 * 256);
            //宽度50.38
            sheet.setColumnWidth(1, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(2, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(3, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(4, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(5, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(6, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(7, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(8, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(9, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(10, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(11, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(12, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(13, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(14, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(15, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(16, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(17, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(18, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(19, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(20, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(21, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(22, ((25 + 1) * 7 + 5) / 7 * 256);


            //创建第一行
            XSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("日期");
            row.createCell(2).setCellValue("新增用户");
            row.createCell(3).setCellValue("新增司机");
            row.createCell(4).setCellValue("新增合计");
            row.createCell(5).setCellValue("用户登录");
            row.createCell(6).setCellValue("司机登录");
            row.createCell(7).setCellValue("登录合计");
            row.createCell(8).setCellValue("下单数");
            row.createCell(9).setCellValue("取消单数");
            row.createCell(10).setCellValue("取消回访数");
            row.createCell(12).setCellValue("重新下单数");
            row.createCell(12).setCellValue("完单数");
            row.createCell(13).setCellValue("人工介入数");
            row.createCell(14).setCellValue("完单总额");
            row.createCell(15).setCellValue("乘客支付");
            row.createCell(16).setCellValue("乘客补贴");
            row.createCell(17).setCellValue("司机补贴");
            row.createCell(18).setCellValue("总补贴");
            row.createCell(19).setCellValue("司机行程数");
            row.createCell(20).setCellValue("总出行人数");
            row.createCell(21).setCellValue("每趟载客数");


            for (int rowNum = 1; rowNum <= list.size(); rowNum++) {
                XSSFRow dataRow = sheet.createRow(rowNum);
                dataRow.createCell(0).setCellValue(rowNum);
                ReportOperationDay itemInfo = list.get(rowNum - 1);
                for (int cellNum = 1; cellNum <= 21; cellNum++) {
                    XSSFCell cell = dataRow.createCell(cellNum);
                    switch (cellNum) {
                        case 1: {
                            //日期
                            cell.setCellValue(itemInfo.getOperationDay());
                            break;
                        }
                        case 2: {
                            //新增用户
                            cell.setCellValue(itemInfo.getNewPassenger());
                            break;
                        }
                        case 3: {
                            //新增司机
                            cell.setCellValue(itemInfo.getNewDriver());
                            break;
                        }
                        case 4: {
                            //新增合计
                            cell.setCellValue(itemInfo.getNewUserTotal());
                            break;
                        }
                        case 5: {
                            //用户登录
                            cell.setCellValue(itemInfo.getPassengerLogin());
                            break;
                        }
                        case 6: {
                            //司机登录
                            cell.setCellValue(itemInfo.getDriverLogin());
                            break;
                        }
                        case 7: {
                            //登录合计
                            cell.setCellValue(itemInfo.getLoginTotal());
                            break;
                        }
                        case 8: {
                            //下单数
                            cell.setCellValue(itemInfo.getCreateOrder());
                            break;
                        }
                        case 9: {
                            //取消单数
                            cell.setCellValue(itemInfo.getCancelOrder());
                            break;
                        }
                        case 10: {
                            //取消回访数
                            cell.setCellValue(itemInfo.getCancelReturnVisit());
                            break;
                        }
                        case 11: {
                            //重新下单数
                            cell.setCellValue((itemInfo.getOrderAgain()));
                            break;
                        }
                        case 12: {
                            //完单数
                            cell.setCellValue(itemInfo.getCompleteOrder());
                            break;
                        }
                        case 13: {
                            //人工介入数
                            cell.setCellValue(itemInfo.getHandOperationOrder());
                            break;
                        }
                        case 14: {
                            //完单总额
                            cell.setCellValue(itemInfo.getCompleteOrderMoney());
                            break;
                        }
                        case 15: {
                            //乘客支付
                            cell.setCellValue(itemInfo.getPassengerPayMoney());
                            break;
                        }
                        case 16: {
                            //乘客补贴
                            cell.setCellValue(itemInfo.getPassengerSubsidy());
                            break;
                        }
                        case 17: {
                            //司机补贴
                            cell.setCellValue(itemInfo.getDriverSubsidy());
                            break;
                        }
                        case 18: {
                            //总补贴
                            cell.setCellValue(itemInfo.getSubsidyTotal());
                            break;
                        }
                        case 19: {
                            //司机行程数
                            cell.setCellValue(itemInfo.getDriverTripTotal());
                            break;
                        }
                        case 20: {
                            //总出行人数
                            cell.setCellValue(itemInfo.getPassengerTotal());
                            break;
                        }
                        case 21: {
                            //每趟载客数
                            cell.setCellValue(itemInfo.getTripPassengerAverage());
                            break;
                        }

                        default:
                            break;
                    }
                }
            }

            response.setHeader("filename", URLEncoder.encode("OperationDay" + ".xlsx", "UTF-8"));
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("OperationDay" + ".xlsx", "UTF-8"));
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            workbook.write(os);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}
